<?php
include "../class/cart.class.php";
include "../class/goods.class.php";
session_start();
$cart = $_SESSION['cart'];
# 重新连接数据库再进行操作
$goods = new Goods("cart", "root", "1234");

$goods_data = $_SESSION['goods_data'];
$cart_data = $cart->getData();

foreach ($goods_data as $goods_row) {
    foreach ($cart_data as $cart_row) {
        if ($goods_row['name'] == $cart_row['id']) {
            $count = $cart_row['num'];
            $left_count = $goods_row['number'];
            if ($count > $left_count) {
                $count = $left_count;
            }
            $name = $cart_row['id'];
            # 数据库删除数量
            $result = $goods->delCount($name, $count);
            if ($result == false) {
                // 应设置错误码
                echo '订单过程失败！';
            }
            break;
        }
    }    
}

# 从商品中删除购物车中的商品，然后清空购物车，并返回
$cart->clear();
header("location: ../index.php");